<template>
  <div class="container">
    <el-container>
      <el-header>
        <div class="header">
          <div class="header-icon">
            <el-icon size="30" v-if="isCollapse" @click="isCollapseBtn">
              <Expand />
            </el-icon>
            <el-icon size="30" v-else @click="isCollapseBtn">
              <Fold />
            </el-icon>
          </div>
          <div class="header-main">
            <!-- 信息弹出框 -->
            <el-popover
              :width="200"
              popper-style="box-shadow: rgb(14 18 22 / 35%) 0px 10px 38px -10px, rgb(14 18 22 / 20%) 0px 10px 20px -15px; padding: 0;"
            >
              <!-- 个人信息 -->
              <template #reference>
                <div class="header-right">
                  <div class="user-img">
                    <el-avatar src="../../public/favicon.ico" />
                  </div>
                  <div class="user-name">{{ userData.userAccount }}</div>
                </div>
              </template>
              <!-- option -->
              <template #default>
                <div
                  class="user-rich-conent"
                  style="display: flex; gap: 16px; flex-direction: column"
                >
                  <ul class="user-option">
                    <li>个人信息</li>
                    <li>设置</li>
                    <li @click="quitLoginFn">退出登录</li>
                  </ul>
                </div>
              </template>
            </el-popover>
          </div>
        </div>
      </el-header>
      <div class="main">
        <div class="left">
          <el-menu
            default-active="1"
            class="el-menu-vertical-demo"
            :collapse="isCollapse"
            :router="true"
          >
            <el-menu-item route="main" index="1">
              <el-icon><icon-menu /></el-icon>
              <template #title>主页</template>
            </el-menu-item>
            <el-sub-menu index="2">
              <template #title>
                <el-icon><location /></el-icon>
                <span>管理页</span>
              </template>
              <el-menu-item-group>
                <el-menu-item
                  v-if="
                    userData.userRole == 'superAdmin' ||
                    userData.userRole == 'admin'
                      ? true
                      : false
                  "
                  route="manage"
                  index="2-1"
                  >接口管理</el-menu-item
                >
              </el-menu-item-group>
              <el-menu-item-group>
                <el-menu-item route="analyze" index="2-2"
                  >接口分析</el-menu-item
                >
              </el-menu-item-group>
            </el-sub-menu>
          </el-menu>
        </div>
        <div class="right">
          <router-view></router-view>
        </div>
      </div>
    </el-container>
  </div>
</template>

<script setup lang="ts">
import { onMounted, reactive, ref } from "vue"
import {
  Document,
  Menu as IconMenu,
  Location,
  Setting,
} from "@element-plus/icons-vue"
import { log } from "console"
import { watch } from "fs"
import { mainStore } from "../stores/stores"
import { getUserInterface, userLogout } from "../request/login-view/login"
import { useRouter } from "vue-router"

const router = useRouter()
const store = mainStore()
const asideWidth = ref(200)
const isCollapse = ref(false)

interface UserDataType {
  id: number
  userName: string | null
  userAccount: string
  userAvatar: string | null
  gender: string | null
  userRole: string
  createTime: string
  updateTime: string
}
let userData: UserDataType = reactive({
  id: 0,
  userName: null,
  userAccount: "",
  userAvatar: null,
  gender: null,
  userRole: "",
  createTime: "",
  updateTime: "",
})

onMounted(() => {
  getUser()
})

// 获取用户信息
const getUser = () => {
  const userid: number = Number(localStorage.getItem("userId"))
  getUserInterface(userid).then((res) => {
    userData = { ...res.data.data }
    console.log(res, 12312)
  })
}

// 退出登录
const quitLoginFn = () => {
  userLogout().then((res) => console.log(res))
  // router.push("/")
}

// 侧边栏展开关闭
const isCollapseBtn = () => {
  isCollapse.value = !isCollapse.value
}
</script>

<style scoped>
.el-menu {
  height: 100% !important;
}
</style>
<style lang="scss" scoped>
.container {
  height: 100%;
  .el-header {
    padding: 0 !important;
    .header {
      display: flex;
      width: 100%;
      .header-main {
        display: flex;
        justify-content: flex-end;
        width: 100%;
        line-height: 60px;
        text-align: center;
        .header-left,
        .header-right,
        .user-img {
          display: flex;
          align-items: center;
          height: 100%;
        }
        .header-left {
          margin-left: 8px;
          padding: 0 20px;
          cursor: pointer;
        }
        .header-right {
          justify-content: end;
          padding-right: 32px;
          .user-name {
            margin-left: 8px;
            cursor: pointer;
          }
          .user-img {
            cursor: pointer;
          }
        }
      }
    }
    .el-icon {
      margin: 15px;
      cursor: pointer;
    }
  }
  .el-container {
    height: 100%;
    .main {
      display: flex;
      height: 100%;
      .right {
        width: 100%;
      }
    }
  }
  .el-menu-vertical-demo:not(.el-menu--collapse) {
    width: 200px;
    min-height: 400px;
  }
}
</style>
<style lang="scss">
.user-rich-conent {
  .user-option {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    box-sizing: border-box;
    width: 100%;
    margin: 0;
    padding: 8px 4px;
    list-style-type: none;
    > li {
      box-sizing: border-box;
      width: 140px;
      height: 32px;
      margin: 4px 0;
      line-height: 32px;
      text-align: center;
      cursor: pointer;
    }
    > li:hover {
      border-bottom: solid 1px rgba(0, 0, 0, 0.312);
    }
  }
}
</style>
